using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.BO.Editoral.Newslist;

using Portal.BO.Editoral.CommentManagement;
using System.Web.Caching;
using System.Xml;
using Portal.User.Security;

namespace Portal.BO.Editoral.Menu
{
	public class MenuCommon
	{
		public static string formatUrlNewsMenu(object urlObj)
		{
			try
			{
				return Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["HostAddress"]) + Convert.ToString(urlObj).Replace("~/", "/");
			}
			catch
			{
				return "";
			}
		}

		public static string getCssClass(object cpmode)
		{
			return (Convert.ToString(cpmode) == Convert.ToString(HttpContext.Current.Session["cpmode"]) ? "Menuleft_Item_Select" : "Menuleft_Item");
		}

		public static string getNewsCountStr(string cpMode)
        //public static string CafeF_GetNewsCountStr(string cpMode, Role objRole)
		{
			string strCpMode = cpMode.ToString().ToLower();
			string cacheName = string.Empty, str = string.Empty;

			switch (strCpMode)
			{
				case "commentmngr":
				case "comment":
					cacheName = "comment_" + HttpContext.Current.User.Identity.Name;
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (string.IsNullOrEmpty(str))
					{
						str = Portal.BO.Editoral.CommentManagement.CommentManagementHelper.CountAllComment();
						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "listcommentthaoluan/3":
					str = Portal.BO.Editoral.CommentManagement.CommentManagementHelper.CountAllCommentThaoLuan();
					break;
				case "list_news_feedback":
					str = Portal.BO.Editoral.BanDocViet.FeedBackNewsHelper.CountFeedBackNews();
					break;
				case "postlist":
					cacheName = "postlist";
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (str == null || str == "")
					{
						Portal.BO.Editoral.BanDocViet.Common objCommon = new Portal.BO.Editoral.BanDocViet.Common();
						str = objCommon.CountBaiBanDocViet(ConfigurationManager.AppSettings["indexFile"]);
						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "feedbacklist":
					cacheName = "feedbacklist";
					str = HttpContext.Current.Cache.Get(cacheName) as string;

					if (str == null || str == "")
					{
						Portal.BO.Editoral.BanDocViet.Common objCommon = new Portal.BO.Editoral.BanDocViet.Common();
						str = objCommon.CountBaiThaoLuan(ConfigurationManager.AppSettings["indexFile"]);

						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "commentthaoluan":
					return Portal.BO.Editoral.CommentManagement.CommentManagementHelper.CountCommentFeedbacknews();
				case "photouserlist":
					cacheName = "photouserlist";
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (str == null)
					{
						//str = Portal.BO.Editoral.PhotoAlbum.PhotoUserHelper.CountPhotoUser("", "0").ToString();
						//HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "allnewstemplist":
					cacheName = "allnewstemplist";
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (str == null)
					{
						str = Portal.BO.Editoral.Newslist.NewslistHelper.News_GetAllNewsTemplistCount("").ToString();
						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "pvsendlist_tkm":
					cacheName = "pvsendlist_tkm_" + HttpContext.Current.User.Identity.Name;
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (str == null)
					{
						//str = Kenh14_BO.BO.Editoral.Newslist.NewslistHelper.GetRowsCount("pvsendlist_tkm").ToString();
						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "pvlistediting_tkm":
					cacheName = "pvlistediting_tkm_" + HttpContext.Current.User.Identity.Name;
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (str == null)
					{
						//str = Kenh14_BO.BO.Editoral.Newslist.NewslistHelper.GetRowsCount("pvlistediting_tkm").ToString();
						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
				case "comment_ngoai":
					cacheName = "comment_ngoai_" + HttpContext.Current.User.Identity.Name;
					str = HttpContext.Current.Cache.Get(cacheName) as string;
					if (str == null)
					{
						//str = ChannelVN.Kenh14Comment.BO.Helper.TongCommentChuaDuyetEx().ToString();
						HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
					}
					break;
                // ====== Count cho Auto Pro ====
                case "commentauto":
                    cacheName = "commentauto_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (string.IsNullOrEmpty(str))
                    {
                        str = AutoPro.BO.CommentManagement.CommentManagementHelper.AlertNewComment();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "hoidaplist":
                    cacheName = "hoidaplist_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (string.IsNullOrEmpty(str))
                    {
                        str = AutoPro.BO.UserComments.UserCommentHelper.AlertNewQuestion();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                // **** End Count Auto Pro ****

				default:
                    if (strCpMode.IndexOf("album") > -1)
                    {
                        //str = PhotoAlbumHelper.CountPhotoAlbums(Convert.ToString(cpMode)).ToString();
                    }
                    else
                    {
                        /* tiennv sua : 20 - 05 - 2010 : lay count tren menu */
                        str = NewslistHelper.GetCountNews(Convert.ToString(cpMode)).ToString();
                        //str = NewslistHelper.CafeF_GetCountNews(cpMode, HttpContext.Current.User.Identity.Name, objRole).ToString();
                    }
					break;
			}
			return str.Equals("0") ? str : int.Parse(str).ToString("#,###");
		}

        public static string CafeF_GetNewsCountStr(string cpMode, Role objRole)
        {
            string strCpMode = cpMode.ToString().ToLower();
            string cacheName = string.Empty, str = string.Empty;

            switch (strCpMode)
            {
                case "commentmngr":
                case "comment":
                    cacheName = "comment_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (string.IsNullOrEmpty(str))
                    {
                        str = Portal.BO.Editoral.CommentManagement.CommentManagementHelper.CountAllComment();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "listcommentthaoluan/3":
                    str = Portal.BO.Editoral.CommentManagement.CommentManagementHelper.CountAllCommentThaoLuan();
                    break;
                case "list_news_feedback":
                    str = Portal.BO.Editoral.BanDocViet.FeedBackNewsHelper.CountFeedBackNews();
                    break;
                case "postlist":
                    cacheName = "postlist";
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (str == null || str == "")
                    {
                        Portal.BO.Editoral.BanDocViet.Common objCommon = new Portal.BO.Editoral.BanDocViet.Common();
                        str = objCommon.CountBaiBanDocViet(ConfigurationManager.AppSettings["indexFile"]);
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "feedbacklist":
                    cacheName = "feedbacklist";
                    str = HttpContext.Current.Cache.Get(cacheName) as string;

                    if (str == null || str == "")
                    {
                        Portal.BO.Editoral.BanDocViet.Common objCommon = new Portal.BO.Editoral.BanDocViet.Common();
                        str = objCommon.CountBaiThaoLuan(ConfigurationManager.AppSettings["indexFile"]);

                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "commentthaoluan":
                    return Portal.BO.Editoral.CommentManagement.CommentManagementHelper.CountCommentFeedbacknews();
                case "photouserlist":
                    cacheName = "photouserlist";
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (str == null)
                    {
                        //str = Portal.BO.Editoral.PhotoAlbum.PhotoUserHelper.CountPhotoUser("", "0").ToString();
                        //HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "allnewstemplist":
                    cacheName = "allnewstemplist";
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (str == null)
                    {
                        str = Portal.BO.Editoral.Newslist.NewslistHelper.News_GetAllNewsTemplistCount("").ToString();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "pvsendlist_tkm":
                    cacheName = "pvsendlist_tkm_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (str == null)
                    {
                        //str = Kenh14_BO.BO.Editoral.Newslist.NewslistHelper.GetRowsCount("pvsendlist_tkm").ToString();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "pvlistediting_tkm":
                    cacheName = "pvlistediting_tkm_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (str == null)
                    {
                        //str = Kenh14_BO.BO.Editoral.Newslist.NewslistHelper.GetRowsCount("pvlistediting_tkm").ToString();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "comment_ngoai":
                    cacheName = "comment_ngoai_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (str == null)
                    {
                        //str = ChannelVN.Kenh14Comment.BO.Helper.TongCommentChuaDuyetEx().ToString();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                // ====== Count cho Auto Pro ====
                case "commentauto":
                    cacheName = "commentauto_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (string.IsNullOrEmpty(str))
                    {
                        str = AutoPro.BO.CommentManagement.CommentManagementHelper.AlertNewComment();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                case "hoidaplist":
                    cacheName = "hoidaplist_" + HttpContext.Current.User.Identity.Name;
                    str = HttpContext.Current.Cache.Get(cacheName) as string;
                    if (string.IsNullOrEmpty(str))
                    {
                        str = AutoPro.BO.UserComments.UserCommentHelper.AlertNewQuestion();
                        HttpContext.Current.Cache.Insert(cacheName, str, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
                    }
                    break;
                // **** End Count Auto Pro ****

                default:
                    if (strCpMode.IndexOf("album") > -1)
                    {
                        //str = PhotoAlbumHelper.CountPhotoAlbums(Convert.ToString(cpMode)).ToString();
                    }
                    else
                    {
                        /* tiennv sua : 20 - 05 - 2010 : lay count tren menu */
                        //str = NewslistHelper.GetCountNews(Convert.ToString(cpMode)).ToString();
                        str = NewslistHelper.CafeF_GetCountNews(cpMode, HttpContext.Current.User.Identity.Name, objRole).ToString();
                    }
                    break;
            }
            return str.Equals("0") ? str : int.Parse(str).ToString("#,###");
        }

		public static XmlDocument getXML()
		{
			string cacheName = "AdminMenu";
			if (HttpContext.Current.Cache[cacheName] != null)
				return (XmlDocument)HttpContext.Current.Cache[cacheName];
			else
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(HttpContext.Current.Server.MapPath("~/GUI/EditoralOffice/MainOffce/menu/menu.xml"));
				HttpContext.Current.Cache.Insert(cacheName, doc, new CacheDependency(HttpContext.Current.Server.MapPath("~/GUI/EditoralOffice/MainOffce/menu/menu.xml")));
				return doc;
			}
		}
	}
}
